import { defineStore } from "pinia";
import { createOne, queryAll, deleteOne, queryOne, updateOne, queryList } from "@/module/hrm/api/job_type";
import Validator from '@/utils/Validator';
const useJobStore = defineStore('job',()=>{
     // 类型列表
     const types = ref([])
     // 获取类型列表
     function getTypes() {
         return new Promise(function (resolve, reject) {
             if (Validator.isEmpty(types.value)) {
                 queryList().then(res => {
                     types.value = res
                     console.log(res)
                     resolve(types.value)
                 })
             } else {
                 //console.log(deptTree.value)``
                 resolve(types.value)
             }
         })
     }
     function freshTypes() {
         return new Promise(function (resolve, reject) {
            queryList().then(res => {
                 types.value = res;
                 //console.log(res)
                 resolve(types.value)
             })
         })
     }
     function addType(data) {
         return new Promise(function (resolve, reject) {
             createOne(data).then(r => {
                queryList().then(res => {
                     types.value = res
                     //console.log(res)
                     resolve(types.value)
                 })
             })
         })
 
     }
     function deleteType(id) {
         return new Promise(function (resolve, reject) {
             deleteOne(id).then(res => {
                queryList().then(res => {
                    types.value = res
                    //console.log(res)
                    resolve(types.value)
                })
             })
         })
     }
     function updateType(data){
        return new Promise(function(resolve,reject){
            updateOne(data).then(r=>{
                queryList().then(res => {
                    types.value = res
                    //console.log(res)
                    resolve(types.value)
                })
            })
        })
     }

     function getType(id){
        return new Promise(function(resolve,reject){
            queryOne(id).then(res=>{
                resolve(res)
            })
        })
     }
     return { types, getTypes, freshTypes, addType, deleteType, updateType, getType }
})
export default useJobStore;